library(lfe)


### edit working directory here: 
setwd("/Users/uqalanau/Dropbox/AirPollutionCognition/Stata") 


braindata <- read.csv("Data/ForFullIV_cluster_inzip.csv")



for(i in 11:26){
  braindata[,i]<-factor(braindata[,i])
  }



###LINEAR PM 

fullivmodel <- felm(
  norm_score ~ weighted_wind_speed + weighted_precipitation | anon_id + monthyear + cut_temp  | (dailypm25 ~ factor(clusterfromb6060dir) +  factor(clusterfromb60120dir) +  factor(clusterfromb60180dir) + factor(clusterfromb60240dir) +  factor(clusterfromb60300dir) 
                                      )   | anon_id + monthyear,  data=braindata,cmethod="reghdfe" ) 

full_results<-summary(fullivmodel)
full_firststage <-summary(fullivmodel$stage1)

save(full_results,full_firststage,file="Results/fullIVresults_cluster_inzip.RData")


###HIGH PM 

fullivmodel_high <- felm(
  norm_score ~ weighted_wind_speed + weighted_precipitation | anon_id + monthyear + cut_temp  | (highpm ~ factor(clusterfromb6060dir) +  factor(clusterfromb60120dir) +  factor(clusterfromb60180dir) + factor(clusterfromb60240dir) +  factor(clusterfromb60300dir) 
                                      )   | anon_id + monthyear,  data=braindata,cmethod="reghdfe" ) 


full_results_high<-summary(fullivmodel_high)
full_firststage_high <-summary(fullivmodel_high$stage1)

save(full_results_high,full_firststage_high,file="Results/fullIVresults_cluster_inziphigh.RData")             




###MODERATE PM 

fullivmodel_mod <- felm(
  norm_score ~ weighted_wind_speed + weighted_precipitation | anon_id + monthyear + cut_temp  | (modpm ~ factor(clusterfromb6060dir) +  factor(clusterfromb60120dir) +  factor(clusterfromb60180dir) + factor(clusterfromb60240dir) +  factor(clusterfromb60300dir) 
                                      )   | anon_id + monthyear,  data=braindata,cmethod="reghdfe" ) 


full_results_mod<-summary(fullivmodel_mod)
full_firststage_mod <-summary(fullivmodel_mod$stage1)

save(full_results_mod,full_firststage_mod,file="Results/fullIVresults_cluster_inzipmod_60.RData")


